<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java方法详解 | 小谭的部落阁</title><meta name="keywords" content="Java,方法详解"><meta name="author" content="谭自财"><meta name="copyright" content="谭自财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="方法详解方法定义设计方法的原则，方法的本意是功能块，就是实现某个功能的语句块的集合。我们设计方法的时候，最好保持方法的原子性，就是一个方法只能完成1个功能，这样有利于我们后期的扩展。 语法修饰符[public private] [static&#x2F;可选] 返回值类型 方法名(参数类型 参数名，参数类型 参数名.....)&amp;#123;  ...    方法体    ...  return 返回值;&amp;#1">
<meta property="og:type" content="article">
<meta property="og:title" content="Java方法详解">
<meta property="og:url" content="https://tanzicai.gitee.io/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/index.html">
<meta property="og:site_name" content="小谭的部落阁">
<meta property="og:description" content="方法详解方法定义设计方法的原则，方法的本意是功能块，就是实现某个功能的语句块的集合。我们设计方法的时候，最好保持方法的原子性，就是一个方法只能完成1个功能，这样有利于我们后期的扩展。 语法修饰符[public private] [static&#x2F;可选] 返回值类型 方法名(参数类型 参数名，参数类型 参数名.....)&amp;#123;  ...    方法体    ...  return 返回值;&amp;#1">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tanzicai.gitee.io/img/WHT.png">
<meta property="article:published_time" content="2022-02-09T12:25:00.000Z">
<meta property="article:modified_time" content="2022-02-09T09:09:23.252Z">
<meta property="article:author" content="谭自财">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="方法详解">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tanzicai.gitee.io/img/WHT.png"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://tanzicai.gitee.io/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java方法详解',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-02-09 17:09:23'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="小谭的部落阁" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/WHT.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小谭的部落阁</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java方法详解</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-09T12:25:00.000Z" title="发表于 2022-02-09 20:25:00">2022-02-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-09T09:09:23.252Z" title="更新于 2022-02-09 17:09:23">2022-02-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java方法详解"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="方法详解"><a href="#方法详解" class="headerlink" title="方法详解"></a>方法详解</h1><h3 id="方法定义"><a href="#方法定义" class="headerlink" title="方法定义"></a>方法定义</h3><p>设计方法的原则，方法的本意是功能块，就是实现某个功能的语句块的集合。我们设计方法的时候，最好保持方法的原子性，就是一个方法只能完成1个功能，这样有利于我们后期的扩展。</p>
<h3 id="语法"><a href="#语法" class="headerlink" title="语法"></a>语法</h3><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">修饰符[<span class="keyword">public</span> <span class="keyword">private</span>] [<span class="keyword">static</span>/可选] 返回值类型 方法名(参数类型 参数名，参数类型 参数名.....)&#123;</span><br><span class="line">  ...</span><br><span class="line">    方法体</span><br><span class="line">    ...</span><br><span class="line">  <span class="keyword">return</span> 返回值;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>示例代码</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01</span> </span>&#123;</span><br><span class="line">    <span class="comment">//main方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> sum = add(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">        System.out.println(sum);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//修饰符 返回类型 参数列表</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a + b;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="方法的定义和调用"><a href="#方法的定义和调用" class="headerlink" title="方法的定义和调用"></a>方法的定义和调用</h3><p>用一些代码片段，来完成特定功能，一般情况下定义一个方法包含以下语法</p>
<ol>
<li>方法包含一个方法头和一个方法体，下面是一个方法的所有部分<ol>
<li><code>修饰符</code>，可选的，告诉编辑器如何调用这个方法，定义了这个方法的<code>访问类型</code></li>
<li><code>返回值类型</code> 方法可能返回值，returnValueType是方法返回值的数据类型， 有些方法执行所需操作，但没有返回值，这种情况下returnValueType是关键字<code>void</code></li>
<li>方法名 方法的实际名称，<code>方法名</code>和<code>参数表</code>共同构成方法前面</li>
<li><code>参数类型</code> 参数像是一个占位符，当方法被调用时，传递值给参数。这个值被称为实参或者变量，参数列表是指<code>参数类型</code>、<code>顺序</code>和<code>参数的个数</code>，参数是可选的，方法可以不包含任何参数<ul>
<li><code>形式参数</code> 在方法被调用时用于接收外界输入的数据</li>
<li><code>实参</code> 调用方法是实际传给方法的数据</li>
</ul>
</li>
<li><code>方法体</code> 方法体包含具体的语句，定义该方法的功能</li>
</ol>
</li>
</ol>
<p>示例代码：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo01</span> </span>&#123;</span><br><span class="line">    <span class="comment">//main方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> sum = add(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">int</span> max = compareMax(<span class="number">3</span>,<span class="number">10</span>);</span><br><span class="line">        System.out.println(sum);</span><br><span class="line">        System.out.println(max);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//修饰符 返回类型 参数列表</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a + b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">compareMax</span><span class="params">(<span class="keyword">int</span> num1, <span class="keyword">int</span> num2)</span></span>&#123;</span><br><span class="line">        <span class="comment">/*比较两个数大小*/</span></span><br><span class="line">        <span class="keyword">if</span> (num1 == num2)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;num1==num2&quot;</span>);</span><br><span class="line">            <span class="keyword">return</span> <span class="number">0</span>;<span class="comment">//终止方法</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (num1&gt;num2)&#123;</span><br><span class="line">            <span class="keyword">return</span> num1;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> num2;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<ul>
<li><code>值传递</code>： 是指在调用函数时，将<code>实际参数</code>复制一份传递到函数值，这样在函数中如果对参数进行修改，将不会影响到实际参数</li>
<li><code>引用传递</code>(pass by reference) 是指在调用函数时，将<code>实际参数</code>的<code>地址</code>直接传递到函数中，那么在函数中对参数所进行的修改，将影响到实际参数。</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line">    pass(i);</span><br><span class="line">    System.out.println(<span class="string">&quot;i=&quot;</span> + i);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">pass</span><span class="params">(<span class="keyword">int</span> j)</span></span>&#123;</span><br><span class="line">    j = <span class="number">20</span>;</span><br><span class="line">    System.out.println(<span class="string">&quot;j=&quot;</span> + j)</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>那么，按照上面的定义，有人得到结论：Java的方法传递是值传递。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    User user = <span class="keyword">new</span> User();</span><br><span class="line">    user.setName(<span class="string">&quot;facker&quot;</span>);</span><br><span class="line">    user.setGender(<span class="string">&quot;Male&quot;</span>);</span><br><span class="line">    pass(user);</span><br><span class="line">    System.out.println(<span class="string">&quot;user =&quot;</span> + user);</span><br><span class="line">&#125;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">pass</span><span class="params">(User user)</span> </span>&#123;</span><br><span class="line">    user.setName(<span class="string">&quot;uzi&quot;</span>);</span><br><span class="line">    System.out.println(<span class="string">&quot;user =&quot;</span> + user);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>经过pass方法执行后，实参的值竟然被改变了，<br>那按照上面的引用传递的定义，实际参数的值被改变了，这不就是<code>引用传递</code>了么。<br>于是，根据上面的两段代码，<br>有人得出一个新的结论：Java的方法中，在传递<code>普通类型</code>的时候是<code>值传递</code>，在传递<code>对象类型</code>的时候是<code>引用传递</code>。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    String name = <span class="string">&quot;facker&quot;</span>;</span><br><span class="line">    pass(name);</span><br><span class="line">    System.out.println(<span class="string">&quot;name = &quot;</span> + name);        <span class="comment">//facker</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pass</span><span class="params">(String name)</span> </span>&#123;</span><br><span class="line">    name = <span class="string">&quot;uzi&quot;</span>;</span><br><span class="line">    System.out.println(<span class="string">&quot;name =&quot;</span> + name);        <span class="comment">//uzi</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>同样传递了一个对象，但是原始参数的值并没有被修改，难道传递对象又变成值传递了？</p>
<p>上面的参数其实是值传递，把实参对象引用的地址当做值传递给了形式参数。<br>所以说，Java中其实还是值传递的，只不过对于对象参数，值的内容是对象的引用</p>
<p>当字符串”cd” 被创建时，java会分配储存字符串所需要的内存量。</p>
<p>然后，对象被分配给了变量x,实际上是将对象的引用分配给了变量x。这个引用是对象储存的内存地址。 变量x包含了一个指向字符串对象的引用，x并不是字符串对象本身。它是一个储存了字符串对象’ab’引用的变量。</p>
<p> java是按值传递的。当x被传递给change()方法时，实际上是x的值（一个引用）的一个副本。</p>
<p>方法change被调用后，会创建另一个对象”cd”，它有着一个不同的引用。方法内的局部变量x的值变成了”cd”的引用。这里改变的是方法内的局部变量的引用值，而不是改不了原先引用的字符串”ab”。</p>
<p> 看图：</p>
<p><img src="https://images2018.cnblogs.com/blog/1371226/201805/1371226-20180503192451104-31203482.jpg"></p>
<h3 id="方法重载"><a href="#方法重载" class="headerlink" title="方法重载"></a>方法重载</h3><p>概念：重载就是在一个类中，有<code>相同的名称</code>，但是<code>形参</code>不同，这个时候就叫做<code>重载</code></p>
<p>方法重载的规则：</p>
<ol>
<li><p><code>方法名字</code>必须相同；</p>
</li>
<li><p><code>参数列表</code>必须<code>不同</code>(<code>个数不同</code> 或者<code>类型不同</code> 或者<code>参数排列顺序不同</code>等)</p>
</li>
<li><p><code>方法返回类型</code>可以<code>相同</code>也<code>可以不同</code></p>
</li>
<li><p>仅仅返回类型不同不足以成为方法的重载。</p>
</li>
</ol>
<p>实现的理论：<code>方法名称相同</code>时，编译器会根据<code>调用方法的参数个数</code>，<code>参数类型</code>等去逐个匹配，以选择对应的方法，如果匹配失败，则编译器报错。</p>
<p>示例代码：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">ReLoadDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> sum1;</span><br><span class="line">        <span class="keyword">float</span> sum2,sum3;</span><br><span class="line">        sum1 = add(<span class="number">3</span>,<span class="number">4</span>);</span><br><span class="line">        sum2 = add(<span class="number">3f</span>,<span class="number">4f</span>,<span class="number">5f</span>);</span><br><span class="line">        sum3 = add(<span class="number">3.45f</span>,<span class="number">2.75f</span>);</span><br><span class="line">        System.out.println(sum1+<span class="string">&quot; &quot;</span>+sum2+<span class="string">&quot; &quot;</span>+sum3);</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">add</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">float</span> <span class="title">add</span><span class="params">(<span class="keyword">float</span> a, <span class="keyword">float</span> b)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">float</span> <span class="title">add</span><span class="params">(<span class="keyword">float</span> a, <span class="keyword">float</span> b, <span class="keyword">float</span> c)</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> a+b+c;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="命令行传参"><a href="#命令行传参" class="headerlink" title="命令行传参"></a>命令行传参</h3><p>有时候我们希望运行一个程序时候，再传递给他消息，这就要靠传递<code>命令参数</code>给<code>main()函数</code>实现。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CommandLine</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;args.length;i++)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;args[&quot;</span>+i+<span class="string">&quot;]:&quot;</span>+args[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h3 id="可变参数"><a href="#可变参数" class="headerlink" title="可变参数"></a>可变参数</h3><p>Java 1.5之后，开始支持传递同类型的可变参数给一个方法</p>
<p>在方法声明中，在指定参数类型后面加一个省略号(<code>......</code>)</p>
<p>一个方法中只能指定<code>一个可变参数</code>，</p>
<p>它必须是方法的最后一个参数，<code>任何普通的参数</code>必须在他的<code>之前</code>声明</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo4</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Demo4 demo4 = <span class="keyword">new</span> Demo4();</span><br><span class="line"></span><br><span class="line">        demo4.method(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">(<span class="keyword">int</span> x, <span class="keyword">int</span>...a)</span></span>&#123;</span><br><span class="line">        System.out.println(x);</span><br><span class="line">        System.out.println(a[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(a[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(a[<span class="number">2</span>]);</span><br><span class="line">        System.out.println(a[<span class="number">3</span>]);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">(<span class="keyword">int</span> i,<span class="keyword">int</span> j)</span></span>&#123;&#125;;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">(<span class="keyword">int</span> i,<span class="keyword">int</span> j,<span class="keyword">int</span> k)</span></span>&#123;&#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="递归调用"><a href="#递归调用" class="headerlink" title="递归调用"></a>递归调用</h3><p>A方法调用B方法我们很容易理解，递归就是A方法调用A方法，就是自己调用自己</p>
<p>利用递归调用可以用简单的程序来解决一些复杂的问题，它通常把一个大型的复杂的问题，层层转化为一个与原问题相似的规模较小的问题来求解，递归策略只需要少量的程序就可以秒输出解题过程所需要的的多次重复计算，大大减少程序的代码量，递归的能力在于用有限的语句来定义对象的无限集合</p>
<p>递归结构一般包含两个部分</p>
<ul>
<li><code>递归头</code> 什么时候不调用自身方法，如果没有递归头将陷入死循环</li>
<li><code>递归体</code>  什么时候需要调用自身方法</li>
</ul>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo6</span> </span>&#123;</span><br><span class="line">    <span class="comment">//2! 2*1</span></span><br><span class="line">    <span class="comment">//3! 3*2*1</span></span><br><span class="line">    <span class="comment">//5! 5*4*3*2*1</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        System.out.println(f(<span class="number">10</span>));</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (n == <span class="number">1</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> n * f(n - <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>一个计算机的代码</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//一个计算器的代码</span></span><br><span class="line"><span class="keyword">package</span> method;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CalcanderDemo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Scanner scanner = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        String ss =<span class="keyword">null</span>; <span class="comment">//初始待输入计算穿</span></span><br><span class="line">        <span class="keyword">char</span> c = <span class="string">&#x27;+&#x27;</span>;  <span class="comment">//初始计算符号</span></span><br><span class="line">        <span class="keyword">double</span> inputNumber=<span class="number">0</span>; <span class="comment">//承载输入的数字</span></span><br><span class="line">        <span class="keyword">double</span> result=<span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (scanner.hasNextLine())&#123;</span><br><span class="line">            ss = scanner.nextLine();</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;你输入的整数是:&quot;</span>+ss);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//示例输入  157-20+10=</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; ss.length(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (ss.charAt(i)&lt;=<span class="string">&#x27;9&#x27;</span>&amp;&amp;ss.charAt(i)&gt;=<span class="string">&#x27;0&#x27;</span>)&#123;</span><br><span class="line">                <span class="comment">//如果字符在0-9之间，就拼接成一个输入数字</span></span><br><span class="line">                inputNumber =inputNumber*<span class="number">10</span>+ss.charAt(i)-<span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="comment">//如果不是数字，那么就是符号，进行符号的选择</span></span><br><span class="line">                <span class="keyword">switch</span> (c)&#123;</span><br><span class="line">                    <span class="keyword">case</span> <span class="string">&#x27;+&#x27;</span>:</span><br><span class="line">                        result =add(result,inputNumber);</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    <span class="keyword">case</span> <span class="string">&#x27;-&#x27;</span>:</span><br><span class="line">                        result =sub(result,inputNumber);</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    <span class="keyword">case</span> <span class="string">&#x27;*&#x27;</span>:</span><br><span class="line">                        result = mul(result,inputNumber);</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                    <span class="keyword">case</span> <span class="string">&#x27;/&#x27;</span>:</span><br><span class="line">                        result = div(result,inputNumber);</span><br><span class="line">                        <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">                inputNumber =<span class="number">0</span>; <span class="comment">//输入数字清空，承接下一个数字</span></span><br><span class="line">                c = ss.charAt(i); <span class="comment">//更改运算符号</span></span><br><span class="line">                <span class="keyword">if</span>(c==<span class="string">&#x27;=&#x27;</span>)&#123;</span><br><span class="line">                    System.out.println(<span class="string">&quot;计算结果是:&quot;</span>+ result);</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">add</span><span class="params">(<span class="keyword">double</span>...num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num.length==<span class="number">0</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;请确认您的输入&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">int</span> result =<span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; num.length; i++) &#123;</span><br><span class="line">            result += num[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">sub</span><span class="params">(<span class="keyword">double</span>...num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num.length==<span class="number">0</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;请确认您的输入&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (num.length==<span class="number">1</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;只有一个参数，该方法需要两个参数&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">double</span> result =num[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; num.length; i++) &#123;</span><br><span class="line">            result -= num[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">mul</span><span class="params">(<span class="keyword">double</span>...num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num.length==<span class="number">0</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;请确认您的输入&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (num.length==<span class="number">1</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;只有一个参数，该方法需要两个参数&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">double</span> result =num[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; num.length; i++) &#123;</span><br><span class="line">            result *= num[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">double</span> <span class="title">div</span><span class="params">(<span class="keyword">double</span>...num)</span></span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (num.length==<span class="number">0</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;请确认您的输入&quot;</span>);</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span> (num.length==<span class="number">1</span>)&#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;只有一个参数，该方法需要两个参数&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">double</span> result =num[<span class="number">0</span>];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt; num.length; i++) &#123;</span><br><span class="line">            result /= num[i];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">谭自财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tanzicai.gitee.io/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/">https://tanzicai.gitee.io/2022/02/09/方法详解/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tanzicai.gitee.io" target="_blank">小谭的部落阁</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/">方法详解</a></div><div class="post_share"><div class="social-share" data-image="/img/WHT.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/"><img class="prev-cover" src="/img/CNYN.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java数组详解</div></div></a></div><div class="next-post pull-right"><a href="/2022/02/09/Java%E6%B5%81%E7%A8%8B/"><img class="next-cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java流程</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/03/21/JavaCore实践-时钟显示/" title="JavaCore实践-时钟显示"><img class="cover" src="/img/survivors_v02_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">JavaCore实践-时钟显示</div></div></a></div><div><a href="/2021/03/21/Java学习路线/" title="Java学习路线"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">Java学习路线</div></div></a></div><div><a href="/2022/02/09/JAVA基础语法/" title="Java知识"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java知识</div></div></a></div><div><a href="/2022/02/10/常用类/" title="Java常用类"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-10</div><div class="title">Java常用类</div></div></a></div><div><a href="/2022/02/09/异常/" title="Java异常"><img class="cover" src="/img/survivors_v03_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java异常</div></div></a></div><div><a href="/2022/02/09/面向对象编程/" title="Java面向对象编程"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java面向对象编程</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">谭自财</div><div class="author-info__description">你别卷入世俗，我别沉迷初遇</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tanzicai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tanzicai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tanzicai" target="_blank" title="Github"><i class="fab fa-git-square"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=1728561744&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fas fa-qq"></i></a><a class="social-icon" href="mailto:1728561744@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">本站正在切换主题测试，如有错误提示，请联系我，谢谢</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3"><span class="toc-number">1.</span> <span class="toc-text">方法详解</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E5%AE%9A%E4%B9%89"><span class="toc-number">1.0.1.</span> <span class="toc-text">方法定义</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%AD%E6%B3%95"><span class="toc-number">1.0.2.</span> <span class="toc-text">语法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E7%9A%84%E5%AE%9A%E4%B9%89%E5%92%8C%E8%B0%83%E7%94%A8"><span class="toc-number">1.0.3.</span> <span class="toc-text">方法的定义和调用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%E9%87%8D%E8%BD%BD"><span class="toc-number">1.0.4.</span> <span class="toc-text">方法重载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C%E4%BC%A0%E5%8F%82"><span class="toc-number">1.0.5.</span> <span class="toc-text">命令行传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8F%AF%E5%8F%98%E5%8F%82%E6%95%B0"><span class="toc-number">1.0.6.</span> <span class="toc-text">可变参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%92%E5%BD%92%E8%B0%83%E7%94%A8"><span class="toc-number">1.0.7.</span> <span class="toc-text">递归调用</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类"><img src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java常用类"/></a><div class="content"><a class="title" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类">Java常用类</a><time datetime="2022-02-10T12:25:00.000Z" title="发表于 2022-02-10 20:25:00">2022-02-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常"><img src="/img/survivors_v03_5120x2880.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java异常"/></a><div class="content"><a class="title" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常">Java异常</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程"><img src="/img/CTTN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java面向对象编程"/></a><div class="content"><a class="title" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程">Java面向对象编程</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解"><img src="/img/CNYN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java数组详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解">Java数组详解</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解"><img src="/img/WHT.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java方法详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解">Java方法详解</a><time datetime="2022-02-09T12:25:00.000Z" title="发表于 2022-02-09 20:25:00">2022-02-09</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('/img/WHT.png')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 谭自财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入 <a href="https://tanzicai.gitee.io">小谭的部落阁</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>